package com.security.sdk.utils;

import com.alibaba.fastjson.JSONObject;
import com.security.sdk.config.JWTConfig;
import com.security.sdk.entity.TokenEntity;
import com.security.sdk.entity.UserEntity;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JWTTokenUtil{

    public static String createToken(UserEntity userEntity, JWTConfig jwtConfig){
        return Jwts.builder()
                //放入用户id
                .setId(String.valueOf(userEntity.getUserId()))
                //主题 - 放入用户名
                .setSubject(userEntity.getUserName())
                //签发时间
                .setIssuedAt(new Date())
                //签发者
                .setIssuer("sa")
                //自定义属性，可放入用户权限
                .claim("authorities", JSONObject.toJSONString(userEntity.getAuthorities()))
                //token失效时间
                .setExpiration(new Date(System.currentTimeMillis() + jwtConfig.getExpiration()))
                //签名算法和密钥
                .signWith(SignatureAlgorithm.HS512, jwtConfig.getSecret())

                .compact();
    }
}
